package com.qboot.quartz.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * Quartz 初始化配置
 *
 * @author Tellsea
 * @date 2023/3/3
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class QuartzInitApplicationListener implements ApplicationListener<ApplicationStartedEvent> {

    private final Scheduler scheduler;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        try {
            scheduler.start();
            scheduler.getListenerManager().addJobListener(new JobLogTriggerListener());
        } catch (SchedulerException e) {
            log.error("Quartz 启动失败");
            throw new RuntimeException(e);
        }
    }
}
